<template>
 <el-dialog 
   width="80%" 
  :close-on-click-modal="false"
  :title="title"
  :model-value="isShow"
  @open="onOpen">
    <el-form
      ref="form"
      :inline="true"
      :model="form"
      :rules="rules"
      label-width="70px"
    >
    <el-form-item label="编码" prop="name">
        <el-input v-model="form.id" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.seq" />
      </el-form-item>
      <el-form-item style="margin-bottom: 0" label="所属应用" prop="type">
        <el-select v-model="form.projectId" placeholder="请选择">
          <el-option
            v-for="item in applications"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    
    </el-form>
    <template #footer>
      <el-button  @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onSave">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>

import { getDetail, add, save } from '@/api/admin/role'
import { getAll as getAllProject } from '@/api/admin/project'
export default {
   components: {  },
  props: {
    data: {
      required: true,
      type: String,
      default: ''
    },
    isShow: {
      require: true,
      type: Boolean,
      default: false
    },
    title: {
      require: true,
      type: String,
      default: ""
    }
  },
  emits: ['onChange','update:isShow'],
  data() {
    return {
      form: {},
      rules: {},
      permissionOptions: {},
      applications: []
      // normalizer(node) {
      //   return {
      //     children: node.child,
      //   };
      // },
    }
  },
  mounted() {
    getAllProject().then(res => {
      this.applications = res.result
    })
    //  getPermission().then((res) => {
    //     this.permissionOptions = res.data
    //   })
  },
  methods: {
    async onOpen() {
      if(this.data) {
        const user = await getDetail(this.data)
        this.form = user.result
      }
      else {
        this.form = {
          name:'',
          description: '',
          sort: 1,
          viewPermissions: [],
          clientId: ''
        }
      }
      
    },
    async onSave() {
      if(this.data) {
        await save(this.form)
      }
      else {
        await add(this.form)
      }
      

      this.$emit("onChange", this.form)
      this.$emit("update:isShow", false)
    },
    onCancel() {
      this.$emit("update:isShow", false)
    },
    onClose() {

    }
  },
  watch: {
    'data': async function() {
      
    }
  }
}
</script>


